﻿using System;
using System.Net;

namespace WebSocketServer
{
    class Program
    {
        static void Main(string[] args)
        {

             EchoAsync();
            Console.ReadKey();

        }
        static async System.Threading.Tasks.Task EchoAsync()
        {
            //新建http服务
            var listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8080/ws");
            listener.Start();
            //获得上下文
            var context = listener.GetContext();
            //等待连接
            var wsContext = await context.AcceptWebSocketAsync(null);
            var ws = wsContext.WebSocket;
            Console.WriteLine("WebSocket connect");
            System.Threading.CancellationToken cancellation = new System.Threading.CancellationToken();
            var buffer = new byte[1024 * 4];
            var wsdata = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), cancellation);
            Console.WriteLine(wsdata.Count);
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer));
            Console.WriteLine("Hello World!");
        }
    }
}
